package com.jdpc.weparty.controller

import com.jdpc.weparty.controller.vo.ActivityInfoVo
import com.jdpc.weparty.controller.vo.JsonBody
import com.jdpc.weparty.service.activity.ActivityService
import com.jdpc.weparty.service.activity.signup.SignUpActivityService
import com.jdpc.weparty.service.user.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*

@RestController
class AdminController {
    @Autowired
    lateinit var activityService: ActivityService

    @Autowired
    lateinit var signUpActivityService: SignUpActivityService

    @Autowired
    lateinit var userService: UserService

    //管理员获取自己需要审批的活动
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/admins/{id}/check")
    fun getCheckingActivities(@PathVariable("id") id: Int, @RequestParam("state") state: Boolean?, @RequestParam("page") page: Int, @RequestParam("size") size: Int): ResponseEntity<*> {
        val examiner = userService.getUser(id)
        val pageable = PageRequest.of(page, size)

        examiner?.let {
            val result = activityService.findActivityByExaminer(it, state, pageable).map {
                ActivityInfoVo(it, 0)
            }
            return ResponseEntity.ok(JsonBody(0, "成功", result))
        } ?: return ResponseEntity.badRequest().body(JsonBody(-1, "未找到该管理员", Any()))
    }

    //管理员审批活动
    @PreAuthorize("hasRole('ADMIN')")
    @PutMapping("/admins/{id}/check")
    fun checkActivity(@RequestParam("activityId") id: Int, @RequestParam("state") state: Boolean?): ResponseEntity<*> {
        activityService.examActivity(id, state)
        return ResponseEntity.ok(JsonBody(0, "成功", Any()))
    }

}